package com.szgr.baoyin.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.szgr.baoyin.R;
import com.szgr.baoyin.activity.AddressActivity;
import com.szgr.baoyin.model.Address;

import java.util.List;

/**
 * Created by Jimbo on 2015/10/11.
 */
public class AddressListAdapter extends BaseAdapter {
    private Context mContext;
    private List<Address> data;
    private View.OnClickListener listener;
    public AddressListAdapter(Context context,List<Address> data){
        mContext = context;
        this.data = data;
    }
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_address_list_view,parent,false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder = (ViewHolder) convertView.getTag();

        Address add = data.get(position);
        holder.tv_name.setText(add.getTrue_name());
        holder.tv_phone.setText(add.getMob_phone());
        holder.tv_address.setText(add.getArea_info() + add.getAddress());
        holder.img_icon.setTag(position);
        holder.img_icon.setOnClickListener(listener);
        holder.img_delete.setTag(position);
        holder.img_delete.setOnClickListener(listener);
        return convertView;
    }

    public void setData(List<Address> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public void setListener(View.OnClickListener listener) {
        this.listener = listener;
    }

    private class ViewHolder {
        TextView tv_name;
        TextView tv_phone;
        ImageView img_icon;
        ImageView img_delete;
        TextView tv_address;

        public ViewHolder(View view){
            tv_name = (TextView) view.findViewById(R.id.item_name);
            tv_phone = (TextView) view.findViewById(R.id.item_phone);
            img_icon = (ImageView) view.findViewById(R.id.item_icon);
            img_delete = (ImageView) view.findViewById(R.id.item_delete);
            tv_address = (TextView) view.findViewById(R.id.item_address);
        }
    }
}
